package com.example.springbott.controller;





import com.example.springbott.aspactJ.OperationName;
import com.example.springbott.bean.*;
import com.example.springbott.bean.common.PasswordBO;
import com.example.springbott.config.MarketToken;
import com.example.springbott.mapper.MarketRoleMapper;
import com.example.springbott.service.AuthService;
import com.example.springbott.util.Count;
import com.example.springbott.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.rmi.activation.ActivationGroup_Stub;
import java.util.ArrayList;
import java.util.Map;

//shiro整合之后，在做具体的开发
//响应结果都是JSON，Controller组件上我们都用@RestController注解
@RestController
@RequestMapping("admin")
public class AuthController {

    @Autowired
    AuthService authService;

    /**
     * Shiro安全框架 → 视频课程 4个课时 → 认证（登录）
     * 如果参数比较少，类型比较简单的话，使用map来接收也可以
     */

    @OperationName("1")
    @PostMapping("auth/login")
    public BaseRespVo<LoginUserData> login(@RequestBody Map map) {
        String username = (String)map.get("username");
        String password = (String)map.get("password");
        // 写业务
        //Subject线程相对安全，只能作为局部变量，是单独的Subject
        //方法里面调用代码
        Subject subject = SecurityUtils.getSubject();
        //执行认证
//        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,password);
        MarketToken authenticationToken = new MarketToken(username,password,Count.TYPE_ADMIN);
        try {
            subject.login(authenticationToken);
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }

        //判断是否认证成功
        LoginUserData loginUserData = new LoginUserData();
        AdminInfoBean adminInfo = new AdminInfoBean();
        if (subject.isAuthenticated()) {
            MarketAdmin principal = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();
            adminInfo.setAvatar(principal.getAvatar());
            adminInfo.setNickName(principal.getUsername());
            Session session = subject.getSession();
            loginUserData.setAdminInfo(adminInfo);
            loginUserData.setToken(session.getId().toString());
            return BaseRespVo.ok(loginUserData);
        }
        return BaseRespVo.unAuthc();

    }
//    @PostMapping("login")
//    public BaseRespVo<LoginUserData> login(@RequestBody Map map) {
//        String username = (String)map.get("username");
//        String password = (String)map.get("password");
//        // 写业务
//        LoginUserData loginUserData = new LoginUserData();
//        AdminInfoBean adminInfo = new AdminInfoBean();
//        adminInfo.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
//        adminInfo.setNickName("admin123");
//        loginUserData.setAdminInfo(adminInfo);
//        loginUserData.setToken("c4d988f3-e4c5-4e46-b6ce-13b9e008ea20");
//        return BaseRespVo.ok(loginUserData);
//    }




    @OperationName("1")
    @RequestMapping("auth/info")
    public BaseRespVo info(String token) {

        InfoData infoData = authService.AuthInfo();
        return BaseRespVo.ok(infoData);
    }




    @OperationName("1")
    @PostMapping("auth/logout")
    public BaseRespVo AdminLogout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok(null);
    }



    @OperationName("1")
    @PostMapping("profile/password")
    public BaseRespVo AdminPassword(@RequestBody PasswordBO passwordBO){
        String oldPassword = passwordBO.getOldPassword();
        MarketAdmin principal = (MarketAdmin) SecurityUtils.getSubject().getPrincipal();
        Integer id = principal.getId();
        int code = authService.AdminPassword(passwordBO,id);
        if (code == Count.SUCCESS_CODE) {
            return BaseRespVo.ok(null);
        }else {
            BaseRespVo<String> stringBaseRespVo = new BaseRespVo<>();
            stringBaseRespVo.setErrno(605);
            stringBaseRespVo.setErrmsg("账号密码不正确");
            return stringBaseRespVo;
        }
    }

}
